let express = require('express');
let router = express.Router();
let actionLog = require('../../common/action_log');
let employeesDao = require('../../model/admin/employees')

let returnNoticeRouter = function (io) {

    //渲染界面
    router.get('/', async function (req, res) {
        let departmentList = await employeesDao.departmentList();
        await res.render('employees/employees', {
            departmentList
        });
    })

    //获取列表数据

    router.get('/list', async function (req, res) {
        let id = req.query.id;
        let page = req.query.page;
        let length = req.query.limit;
        let start = (page - 1) * length;
        let employeesList = await employeesDao.employeesList(start, length, id);
        let count = employeesList[1][0].sum;
        let data = employeesList[0];
        await res.json({
            code: 0,
            count: count,
            data: data,
            msg: 'success'
        })
    })

    //渲染员工添加界面

    router.get('/add', async function (req, res) {
        let departmentList = await employeesDao.departmentList();
        await res.render('employees/add', {
            departmentList
        })
    })

    //员工添加提交事件

    router.post('/add', async function (req, res) {
        let host = req.headers.origin;
        let num = req.body.num;
        let name = req.body.name;
        let department = req.body.department;
        let phone = req.body.phone;
        let img = req.body.imgurl;
        if (!num) {
            return await res.json({
                status: 203,
                msg: '请输入员工工号！'
            })
        }
        if (!name) {
            return await res.json({
                status: 203,
                msg: '请输入员工姓名！'
            })
        }
        if (!department) {
            return await res.json({
                status: 203,
                msg: '请输入员工所属部门！'
            })
        }
        if (!phone) {
            return await res.json({
                status: 203,
                msg: "请输入员工联系方式！"
            })
        }
        if (!img) {
            return await res.json({
                status: 203,
                msg: '请上传员工人像图！'
            })
        }
        //判断员工工号是否重复
        let judgeNum = await employeesDao.judgeNum(num);
        if (judgeNum.length > 0) {
            return await res.json({
                status: 203,
                msg: '员工工号重复！'
            })
        }
        let imgUrl = host + img;
        let str = department.split("+");
        let departmentId = str[0];
        let departmentName = str[1];

        await employeesDao.addEmployees(num, name, departmentId, departmentName, phone, imgUrl)
        //生成操作日志
        let action = 'imageAdd';
        let actionContent = '新增员工' + name;
        await actionLog.crearteActionLog(req, action, actionContent);
        await res.json({
            status: 200,
            msg: 'success'
        })
    })

    //渲染员工详情界面
    router.get('/detail/:id', async function (req, res) {
        let id = req.params.id;
        let employeesDetail = await employeesDao.employeesDetail(id);
        await res.render('employees/detail', {
            employeesDetail
        });
    })
    //渲染员工修改界面

    router.get('/edit/:id', async function (req, res) {
        let id = req.params.id;
        let employeesDetail = await employeesDao.employeesDetail(id);
        let departmentList = await employeesDao.departmentList();
        await res.render('employees/edit', {
            employeesDetail,
            departmentList
        });
    })

    //员工信息修改
    router.post('/edit', async function (req, res) {
        let host = req.headers.origin;
        let num = req.body.num;
        let name = req.body.name;
        let department = req.body.department;
        let phone = req.body.phone;
        let img = req.body.imgurl;
        let id = req.body.id;
        if (!num) {
            return await res.json({
                status: 203,
                msg: '请输入员工工号！'
            })
        }
        if (!name) {
            return await res.json({
                status: 203,
                msg: '请输入员工姓名！'
            })
        }
        if (!department) {
            return await res.json({
                status: 203,
                msg: '请输入员工所属部门！'
            })
        }
        if (!phone) {
            return await res.json({
                status: 203,
                msg: "请输入员工联系方式！"
            })
        }
        if (!img) {
            return await res.json({
                status: 203,
                msg: '请上传员工人像图！'
            })
        }
        //判断员工的工号是否重复
        let judgeEditNum = await employeesDao.judgeEditNum(num, id);
        if (judgeEditNum.length > 0) {
            return await res.json({
                status: 203,
                msg: '员工工号重复'
            });
        }
        let judgeHttp = img.indexOf('http'); //判断图片链接是否拼接
        let imgUrl = img;
        if (judgeHttp < 0) {
            imgUrl =host + img;
        }
        let str = department.split("+");
        let departmentId = str[0];
        let departmentName = str[1];
        await employeesDao.updateEmployees(id, num, name, departmentId, departmentName, imgUrl);
        await res.json({
            status: 200,
            msg: "success"
        })
        let editDetial = {};
        editDetial.id = id;
        editDetial.name = name;
        editDetial.url = imgUrl;
        let results = {};
        results.action = 'images update';
        results.data = editDetial;
        io.sockets.emit('message', results);

        let action = 'imageUpdate';
        //生成操作日志
        let actionContent = '编辑员工信息' + name;
        await actionLog.crearteActionLog(req, action, actionContent);


    })
    //员工信息删除
    router.post('/del', async function (req, res) {
        let id = req.body.id;
        let data = {};
        data.name = req.body.name;
        data.url = req.body.url;
        data.isdelete = req.body.isdelete;
        data.id = id;
        await employeesDao.removeEmployees(id);

        let results = {};
        results.action = 'images delete';
        results.data = data;
        io.sockets.emit('message', results);
        await res.json({
            status: 200,
            msg: "success"
        })

        //生成操作日志

        let action = 'imageDel'
        let actionContent = '删除编号为' + id + '员工';
        await actionLog.crearteActionLog(req, action, actionContent);
    })


    return router;
}

module.exports = returnNoticeRouter;